﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SService
{
    public class PurchaseordersS : PurchaseordersSI
    {
        private readonly WmssystemContext myDb;

        public PurchaseordersS(WmssystemContext myDb)
        {
            this.myDb = myDb;
        }
        public Task<List<PO_Purchaseorders>> Fye(int pageSize, int currentPage, string Number = null, string Status = null, string Supplier = null, DateTime? date = null, DateTime? date1 = null)
        {
            var query = myDb.PO_Purchaseorders.AsQueryable();

            // 根据条件进行查询
            if (!string.IsNullOrEmpty(Number))
            {
                query = query.Where(c => c.P_OOrderNumber.Contains(Number));
            }
            if (!string.IsNullOrEmpty(Status))
            {
                query = query.Where(c => c.P_OStatus == Status);
            }
            if (!string.IsNullOrEmpty(Supplier))
            {
                query = query.Where(c => c.P_OSupplier == Supplier);
            }
            if (date != null)
            {
                query = query.Where(e => e.P_ODate >= date);
            }
            if (date1 != null)
            {
                query = query.Where(e => e.P_ODate <= date1);
            }
            query = query.OrderByDescending(e => e.P_OIndex);
            // 分页查询
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        public Task<int> JhAdd(PO_Purchaseorders pO_Purchaseorders)
        {
            myDb.PO_Purchaseorders.Add(pO_Purchaseorders);
            return myDb.SaveChangesAsync();
        }

        public async Task<int> JhDel(List<int> Ids)
        {
            // 使用 LINQ 查询语句检索相应的记录
            var detailsToDelete = await myDb.PO_Purchaseorders
                .Where(d => Ids.Contains(d.P_OIndex))
                .ToListAsync();

            // 删除查询结果
            myDb.PO_Purchaseorders.RemoveRange(detailsToDelete);

            // 保存删除操作到数据库
            return await myDb.SaveChangesAsync();
        }

        public async Task<int> JhShen(PO_Purchaseorders pO_Purchaseorders, string to)
        {
            // 1. 根据指定条件查找要审核的记录
            var purchaseOrder = await myDb.PO_Purchaseorders.FindAsync(pO_Purchaseorders.P_OIndex);

            if (purchaseOrder == null)
            {
                // 如果没有找到对应的记录，可以进行适当的处理，比如抛出异常或者返回错误码
                throw new Exception("指定的记录不存在");
            }
            if (to == "通过")
            {
                purchaseOrder.P_OStatus = "未入库";
            }
            else
            {
                purchaseOrder.P_OStatus = "待审核";
            }
            // 2. 更新找到的记录的相应字段
            purchaseOrder.P_OApprover = "admin"; // 以此类推，更新需要修改的字段
            purchaseOrder.P_OApproverTime =DateTime.Now;
            purchaseOrder.P_Reviewcomments = pO_Purchaseorders.P_Reviewcomments;
           

            // 3. 将更新后的记录保存回数据库中
            myDb.PO_Purchaseorders.Update(purchaseOrder);
            return await myDb.SaveChangesAsync(); // 返回受影响的行数
        }

        public async Task<int> JhUpd(PO_Purchaseorders pO_Purchaseorders)
        {
            // 1. 根据指定条件查找要修改的记录
            var purchaseOrder = await myDb.PO_Purchaseorders.FindAsync(pO_Purchaseorders.P_OIndex);

            if (purchaseOrder == null)
            {
                // 如果没有找到对应的记录，可以进行适当的处理，比如抛出异常或者返回错误码
                throw new Exception("指定的记录不存在");
            }

            // 2. 更新找到的记录的相应字段
            purchaseOrder.P_ODate = pO_Purchaseorders.P_ODate; // 以此类推，更新需要修改的字段
            purchaseOrder.P_OContactPerson = pO_Purchaseorders.P_OContactPerson;
            purchaseOrder.P_OContactInformation = pO_Purchaseorders.P_OContactInformation;
            purchaseOrder.P_OReamr = pO_Purchaseorders.P_OReamr;

            // 3. 将更新后的记录保存回数据库中
            myDb.PO_Purchaseorders.Update(purchaseOrder);
            return await myDb.SaveChangesAsync(); // 返回受影响的行数
        }

        public Task<List<PO_Purchaseorders>> Qu()
        {
            return myDb.PO_Purchaseorders.ToListAsync();
        }
    }
}
